﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Drawing;
using System.IO;

namespace ANGFLib
{
    /// <summary>
    /// この属性が付加されたクラスは、CollectTypedObjectsメソッドの収集から除外されます
    /// </summary>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]
    public class ExcludeAttribute : Attribute { }

    /// <summary>
    /// オプションの便利なサービスを提供する
    /// 使っても使わなくても良い
    /// </summary>
    public static class Util
    {
        /// <summary>
        /// 現在のアセンブリはAssembly.GetExecutingAssembly()で取る
        /// </summary>
        /// <typeparam name="TargetType"></typeparam>
        /// <param name="targetAssebmbly"></param>
        /// <returns></returns>
        public static TargetType[] CollectTypedObjects<TargetType>(Assembly targetAssebmbly)
        {
            List<TargetType> list = new List<TargetType>();
            foreach (var t in targetAssebmbly.GetTypes())
            {
                if (t.IsAbstract) continue;
                if (t.IsSubclassOf(typeof(TargetType)))
                {
                    if (t.GetCustomAttributes(typeof(ExcludeAttribute), false).Length == 0)
                    {
                        list.Add((TargetType)Activator.CreateInstance(t));
                    }
                }
            }
            return list.ToArray();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="c1"></param>
        /// <param name="c2"></param>
        /// <returns></returns>
        public static Color CalcCenterColor(Color c1, Color c2)
        {
            int r = c1.R + (c2.R - c1.R) / 2;
            int g = c1.G + (c2.G - c1.G) / 2;
            int b = c1.B + (c2.B - c1.B) / 2;
            return Color.FromArgb(r, g, b);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="c1"></param>
        /// <param name="c2"></param>
        /// <param name="rate"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static Color CalcBetweenColor(Color c1, Color c2, int rate, int count)
        {
            int r = c1.R + (c2.R - c1.R) * rate / count;
            int g = c1.G + (c2.G - c1.G) * rate / count;
            int b = c1.B + (c2.B - c1.B) * rate / count;
            return Color.FromArgb(r, g, b);
        }
    }

    /// <summary>
    /// バイナリ配列を作成する
    /// </summary>
    public class BinaryArrayBuilder
    {
        private Stream stream;
        private List<byte> list = new List<byte>();
        /// <summary>
        /// 配列を追加する
        /// </summary>
        /// <param name="arToAdd"></param>
        public void AppendArray(byte[] arToAdd)
        {
            list.AddRange(arToAdd);
        }
        /// <summary>
        /// 配列に変換する
        /// </summary>
        /// <returns></returns>
        public byte[] ToArray()
        {
            return list.ToArray();
        }
        /// <summary>
        /// コンストラクタ。
        /// </summary>
        /// <param name="stream">ダミー</param>
        public BinaryArrayBuilder(Stream stream)
        {
            this.stream = stream;
        }
    }
}
